<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板2.0</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #main {
            width: 500px;
            height: 500px;
            margin: 50px auto;
        }

        li {
            list-style: none;
        }

        #talks li {
            margin-bottom: 10px;
            background-color: pink;
            color: #f00;
        }

        #btn {
            margin: 30px 100px;
        }
    </style>
</head>

<body>
    <div id="main">
        <textarea name="" id="talk" cols="50" rows="5">请输入</textarea>
        <br>
        <input id="btn" type="button" value="发布">
        <ul id="talks">
            <li></li>
        </ul>
    </div>

    <script>
        //文本框获得焦点，清除提示信息
        //文本框失去焦点，没有内容，还原提示信息
        var txt = document.querySelector('#talk');
        txt.onfocus = function () {
            this.value = '';
        }
        txt.onblur = function () {
            if (!this.value) {
                this.value = '请输入';
            }
        }
        var btn = document.querySelector('#btn')
        btn.onclick = function () {
            var li = document.createElement('li');
            li.innerHTML = txt.value;
            //li的内容后面增加一个删除超链接
            var a = document.createElement('a');
            a.href = 'JavaScript:delete(this)';//超链接不需要跳转
            a.innerHTML = '删除';
            a.style.float = 'right';
            a.onclick = function () {
                //删除超链接对应的留言
                // console.log(this);
                var li = this.parentNode;
                li.parentNode.removeChild(li);
            }
            li.appendChild(a);

            var ul = document.querySelector('#talks');
            ul.appendChild(li);
            // ul.insertBefore(li,ul.children[1]);
        }
    </script>
</body>

</html>